/**
* Servlet to manage input values request actions for idbcrown
* This servlet should be called like this:
*    Called to insert values for a known device:
*       http://localhost:3141/idbcrownINServer/idbcrownINServer?action=INSERT_VALUE&InfoDevice=Circle&InfoMeasure=PI&InfoValue=3.889
*    Called to get values for a known device:
*       http://localhost:3141/idbcrownINServer/idbcrownINServer?action=GET_VALUES&InfoDevice=IR&InfoMeasure=Heat
* It must be configured in this way:
*
*     <web-app xmlns="http://java.sun.com/xml/ns/javaee"
*     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
*        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
*        version="2.5"> 
*     
*       <display-name>idbcrownINServer</display-name>
*       <description>idbcrownINServer</description>
*     
*       <servlet>
*         <servlet-name>idbcrownINServer</servlet-name>
*         <servlet-class>idbcrownINServer</servlet-class>
*         <load-on-startup>1</load-on-startup>
*       </servlet>
*     
*       <servlet-mapping>
*         <servlet-name>idbcrownINServer</servlet-name>
*     	  <url-pattern>/idbcrownINServer/*</url-pattern>
*       </servlet-mapping>
*     
*       <session-config>
*         <session-timeout>600</session-timeout>
*       </session-config>
*     </web-app>
*
* Project: idbcrown
* Author:  TM
* Date:    22/12/2012
*
* History
* ------------------
* 22/12/2012 - TM - Initial version.
*/

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import Application.*;
import Functions.*;
import comm.*;
import Util.*;

/*
 * Inserting record into a population table
 */

public class idbcrownINServer extends HttpServlet implements constantsInterface
{
    /** Constants */
    protected static String GET_CALL  = "GET";
    protected static String POST_CALL = "POST";
    /** Enter point for menu */
    protected String action;
    
    /** Method Get*/
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
       createAction(request, response, GET_CALL);
    }
    
    /** Main doGet  */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
       createAction(request, response, POST_CALL);
    }
    
    /** Create action after request */
    public void createAction(HttpServletRequest request, HttpServletResponse response, String typeCall) throws IOException, ServletException
    {
      response.setContentType("text/html");
      action = request.getParameter(prefixAction);
      genericFunction function = null;
      
      if (action == null)
      {
        writeResponse(response, "Invalid action", typeCall);  
        return;
      }
      
      //Checking different actions
      if (action.equalsIgnoreCase(insertAction))
      {
        function = new insertValue(request, response);
      }
      else if (action.equalsIgnoreCase(getValuesAction))
      {
        function = new getValuesFromDevice(request, response);
      }
      else if (action.equalsIgnoreCase(isUserOk))
      {
        function = new login(request, response);         
      }
      else
        writeResponse(response, "Unknown call for "+action, typeCall);  
      
      //Executing action
      if (function != null)
      {
        String result = function.execute();        
        function.sendResponse(result);
      }
    }

    /** Write content in output web page */
    protected void writeResponse(HttpServletResponse response, String result, String typeCall) throws IOException, ServletException
    {
       if (!typeCall.equals(GET_CALL))
         return;
         
       PrintWriter out  = response.getWriter();
       htmlTools   html = new htmlTools(out);
       
       html.printHeader();
       html.printBody  (result);
       html.printFooter();
    }
    
}